package com.wxtl.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.wxtl.action.base.BaseAction;
import com.wxtl.entity.Agent;
import com.wxtl.entity.Dog;
import com.wxtl.exception.ServiceException;
import com.wxtl.service.AgentService;
import com.wxtl.service.DogService;
import com.wxtl.util.Constants;
import com.wxtl.util.MD5Util;
import com.wxtl.util.Page;
import com.wxtl.util.Status;

public class AgentManageAction extends BaseAction {
	private AgentService agentService;
	private DogService dogService;
	private Dog dog=new Dog();
	private Agent agent=new Agent();
	private String npassword;
	
	public Dog getDog() {
		return dog;
	}

	public void setDog(Dog dog) {
		this.dog = dog;
	}
	

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	

	public String getNpassword() {
		return npassword;
	}

	public void setNpassword(String npassword) {
		this.npassword = npassword;
	}

	public void setAgentService(AgentService agentService) {
		this.agentService = agentService;
	}

	public void setDogService(DogService dogService) {
		this.dogService = dogService;
	}
	
	/**
	 * 返回代理商的加密狗列表
	 * @return
	 */
	public String queryDog(){
		dog.setAgentsn("A0001");
		Page pageData=dogService.query(super.getDataGrid(), dog, 0);
		setPageData(pageData);
		return SUCCESS;
	}
	
	/**
	 * 得到代理商的产品列表
	 * @return
	 */
	public String queryProduct(){
		//代理商编号应该在session中获得
		Page pageData=agentService.queryProduct(super.getDataGrid(), 1000);
		setPageData(pageData);
		return SUCCESS;
	}
	
	/**
	 * 代理人信息修改
	 * @return
	 */
	public String updateAgent(){
		Agent agent1=agentService.queryAgentById(agent.getAgentId());//获得代理人信息
		agent1.setArea(agent.getArea());
		agent1.setChannelMgr(agent.getChannelMgr());
		agent1.setLinkman(agent.getLinkman());
		agent1.setName(agent.getName());
		agent1.setTel(agent.getTel());
		
		agentService.update(agent1);
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(Constants.ADMIN_SESSION_KEY, agent1);//更新session
		return SUCCESS;
	}
	
	public String updatePwd(){
		//验证旧密码
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object obj= session.getAttribute(Constants.ADMIN_SESSION_TYPE);
		int loginType=-1;
		if(obj!=null){
			loginType=Integer.parseInt(obj.toString());
		}
		if(loginType==Status.AGENT_ACCOUNT){
			Agent agent1= (Agent) session.getAttribute(Constants.ADMIN_SESSION_KEY);
			if(!agent1.getPassword().equals(MD5Util.getMD5String(agent.getPassword()+Constants.SALT))){
				throw new ServiceException("旧密码输入错误");
			}
			agent1.setPassword(npassword);
			agent1.setPassword(MD5Util.getMD5String(agent1.getPassword()+Constants.SALT));//密码加密
			agentService.update(agent1);
		}
		return SUCCESS;
	}
	
	public String queryAgent(){
		//查询当前登录的代理商
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object obj= session.getAttribute(Constants.ADMIN_SESSION_TYPE);
		int loginType=-1;
		if(obj!=null){
			loginType=Integer.parseInt(obj.toString());
		}
		if(loginType==Status.AGENT_ACCOUNT){
			agent= (Agent) session.getAttribute(Constants.ADMIN_SESSION_KEY);
		}
		return SUCCESS;
	}
}
